<?php 

/**
 * Class that represents settings storage.
 *
 */
final class WereWordConfig
{
    /**
     * Config cache.
     *
     * @var array
     */
    /*
    private static $config;
	*/
    
    /**
     * List of default blog setting values
     *
     * @var array
     */
    private static $defaults = array (
    	'blog.name'=>'WereWord',
        'blog.owner_login' => 'admin',
        'blog.owner_password' => '2c6b1258010a248d217da5d04db9db6de1d1', // 'setup'
        'blog.owner_name' => 'Author Name',
        'blog.owner_email' => 'example@example.com',
        'blog.index_posts_count' => '3',
        'blog.pictures.medium_preview_width' => '640',
        'blog.pictures.medium_preview_height' => '640',
        'blog.pictures.small_preview_width' => '150',
        'blog.pictures.small_preview_height' => '150',
        'blog.applet.links.links_list' => "Link 1 text|http://example.com\nLink 2 text|http://example.net",
        'blog.notification_to_owner_template_body' => "New comment has been added recently  in reply to post “%post_title%” (%post_url%)
Approve required: %approve_required%
Comment URL: %comment_url%
Comment text:\n%comment%",
        'blog.notification_to_owner_template_subject' => 'New comment added',
        'blog.openid_server' => '',
        'openid_server_delegate' => '',
    );
    
    /**
     * Database connection PDO 
     */
    private static $con = null;
    
    /**
     * Get config value.
     *
     * @param string $name name of the config element
     * @return mixed
     */
    public static function get($name)
    {
        $setting = Doctrine::getTable('BlogSetting')->find($name);
        if ($setting === false) {
            if (isset(self::$defaults[$name])) {
                return self::$defaults[$name];
            } else {
                return null;
            }
        }
        
        return $setting->value;
    }
    
    /**
     * Get list of config values
     *
     * @param array $names
     */
    public static function mget($names)
    {
        if (!is_array($names)) {
            return null;
        }
        $res = array();
        foreach ($names as $name) {
            $res[] = self::get($name);
        }
        
        return $res;
    }
    
    /**
     * Set config value
     *
     * @param string $name
     * @param mixed $value
     * @throws Doctrine_Exception
     */
    public static function set($name, $value)
    {
        $con = self::pdo();
        $con->beginTransaction();
        $setting = Doctrine::getTable('BlogSetting')->find($name);
        
        try {
            if (!$setting instanceof BlogSetting) {
                $setting = new BlogSetting();
                $setting->name = $name;
            }
            $setting->value = $value;
            $setting->save($con);
            $con->commit();
        } catch (Doctrine_Exception $e) {
            $con->rollback();
            throw $e;
        }
    }
    
    /**
     * Get database connection object.
     *
     * @return DoctrinePDO
     */
    private static function pdo()
    {
        if (is_null(self::$con)) {
            self::$con = AgaviContext::getInstance('web')->getDatabaseManager()->getDatabase('blog')->getConnection();
        }
        
        return self::$con;
    }
}
?>
